package com.campus.marketplace.controller;

import com.campus.marketplace.entity.Comment;
import com.campus.marketplace.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/comments")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @PostMapping
    public ResponseEntity<Comment> addComment(@RequestBody Comment comment) {
        return ResponseEntity.ok(commentService.addComment(comment));
    }

    @PostMapping("/{parentId}/reply")
    public ResponseEntity<Comment> replyComment(
            @PathVariable Long parentId,
            @RequestBody Comment comment) {
        return ResponseEntity.ok(commentService.replyComment(parentId, comment));
    }

    @GetMapping("/product/{productId}")
    public ResponseEntity<List<Comment>> getProductComments(@PathVariable Long productId) {
        return ResponseEntity.ok(commentService.getProductComments(productId));
    }

    @GetMapping("/{commentId}/replies")
    public ResponseEntity<List<Comment>> getCommentReplies(@PathVariable Long commentId) {
        return ResponseEntity.ok(commentService.getCommentReplies(commentId));
    }

    @PutMapping("/{commentId}")
    public ResponseEntity<Comment> updateComment(
            @PathVariable Long commentId,
            @RequestParam String content) {
        return ResponseEntity.ok(commentService.updateComment(commentId, content));
    }

    @DeleteMapping("/{commentId}")
    public ResponseEntity<Void> deleteComment(@PathVariable Long commentId) {
        commentService.deleteComment(commentId);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/{commentId}")
    public ResponseEntity<Comment> getCommentById(@PathVariable Long commentId) {
        return ResponseEntity.ok(commentService.getCommentById(commentId));
    }

    @GetMapping("/pending")
    public ResponseEntity<List<Comment>> getPendingComments() {
        return ResponseEntity.ok(commentService.getPendingComments());
    }

    @PostMapping("/{commentId}/approve")
    public ResponseEntity<Void> approveComment(@PathVariable Long commentId) {
        commentService.approveComment(commentId);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/{commentId}/reject")
    public ResponseEntity<Void> rejectComment(@PathVariable Long commentId) {
        commentService.rejectComment(commentId);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/product/{productId}/count")
    public ResponseEntity<Long> getCommentCount(@PathVariable Long productId) {
        return ResponseEntity.ok(commentService.getCommentCount(productId));
    }
}